package com.srwl.mytx.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.srwl.mytx.R;

/**
 * 搜索view
 */
public class AddressSearchView extends ConstraintLayout implements View.OnClickListener {
    private Context context;
    public EditText et_content;
    private ImageView iv_contentClear;
    public TextView tv_search;
    private InteractionListener listener;
    private boolean search;


    public AddressSearchView(Context context) {
        super(context);
        this.context = context;
        init(null, 0);
    }

    public AddressSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs, 0);
    }

    public AddressSearchView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init(attrs, defStyle);
    }


    private void init(AttributeSet attrs, int defStyle) {
        inflate(context, R.layout.address_search_view, this);
        et_content = findViewById(R.id.et_content);
        iv_contentClear = findViewById(R.id.iv_content_clear);
        tv_search = findViewById(R.id.tv_search);
        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    tv_search.setText("搜索");
                    search = true;
                } else {
                    tv_search.setText("确定");
                    search = false;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    iv_contentClear.setVisibility(VISIBLE);
                } else {
                    iv_contentClear.setVisibility(INVISIBLE);
                }
            }
        });

        et_content.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                String content = et_content.getText().toString();
                if (content.length() == 0) return true;
                if (listener != null) {
                    if (search) {
                        listener.onSearch(content);
                    } else {
                        listener.onConfirm();
                    }
                }
                return true;
            }
        });

        iv_contentClear.setOnClickListener(this);
        tv_search.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_content_clear:
                et_content.setText("");
                break;
            case R.id.tv_search:
                String content = et_content.getText().toString();
                if (listener != null) {
                    if (search) {
                        listener.onSearch(content);
                    } else {
                        listener.onConfirm();
                    }
                }
                break;
        }
    }

    public void onSearchResultSelected() {
        tv_search.setText("确定");
        search = false;
    }

    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    public interface InteractionListener {
        void onSearch(String content);

        void onConfirm();
    }

}
